package com.example.demo.service;

import com.example.demo.common.base.PageQo;
import com.example.demo.common.constants.SystemConstant;
import com.example.demo.common.enums.ApiErrorEnum;
import com.example.demo.common.wrapper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.persistence.MappedSuperclass;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author xcy
 * @Desc
 * @date 2021/3/23 23:47
 * @Version v1.0
 */
@MappedSuperclass
public class BaseService {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 包装数据到自定义分页
     */
    @SuppressWarnings("unchecked")
    protected Page wrap(PageInfo pageInfo){
        Page page = new Page();
        if(pageInfo != null){
            page.setPages(pageInfo.getPages());
            page.setTotal(pageInfo.getTotal());
            page.setRows(pageInfo.getList());
        }
        return page;
    }

    /**
     * 分页
     */
    protected void startPage(PageQo qo){
        PageHelper.startPage(qo.getPage() == null ? 0 : qo.getPage(), qo.getRows() == null ? 5 : qo.getRows());
    }



    /**
     * 获取token
     * @return Token
     */
    //protected Token getToken(){
    //    HttpServletRequest request = getRequest();
    //    return (Token) request.getAttribute(RequestConstant.TOKEN);
    //}

    /**
     * 获取请求
     */
    //protected HttpServletRequest getRequest(){
    //    ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes() ;
    //    HttpServletRequest request =  sra.getRequest();
    //    if(request == null){
    //        throwOpenApiException(ApiErrorEnum.API_USER_3);
    //    }
    //    return request;
    //}
}